<!DOCTYPE html>
<html lang="zh-cn" xmlns="https://www.w3.org/1999/html">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>商户后台</title>  <link rel="icon" href="/template/pc1/images/wmh_favicon.ico" type="image/x-icon" />
    <!-- Bootstrap -->
    <link href="/res/plugins/weixin/weixinmenu/bootstrap_min.css" rel="stylesheet" media="all"/>
    <link href="/res/plugins/weixin/weixinmenu/bootstrap.min.css" rel="stylesheet">
    <link href="/res/plugins/weixin/weixinmenu/style.css" rel="stylesheet">
    <link href="/res/plugins/weixin/weixinmenu/style_new.css" rel="stylesheet">
    <link href="/res/plugins/weixin/weixinmenu/style01.css"  rel="stylesheet" type="text/css" media="all"/>
    <script src="/res/js/jquery.js" type="text/javascript" ></script>
    <script src="/res/plugins/weixin/weixinmenu/bootstrap_min.js" type="text/javascript"></script>
    <link href="/res/css/common.css" rel="stylesheet" type="text/css" />

    <style type="text/css">
        .col-sm-18{
            margin-left: 20px;
        }
        .input-medium{
            margin-left: -13px;
        }

    </style>
</head>
<body>
<div class="path">
    微信管理 &raquo; 自定义菜单管理
</div>
<div class="container-fluid page_body" style="padding-top: 1px">
    <div class="row" style="margin-left: 0px">
        <div class="main_cont" style="height: 904px;">
            <div id="main">
                <div class="container-fluid">

                    <div class="row-fluid">
                        <div class="span12">

                            <div class="box">
                                <div class="box-content" style="padding-left: 0px">
                                    <div class="alert">
                                        <p>注意：1级菜单最多只能开启3个，2级子菜单最多开启5个!</p>
                                        <p>只有保存主菜单后才可以添加子菜单</p>
                                        <p>删除1级菜单时,其下面的所有子菜单也会被删除</p>
                                        <p>生成自定义菜单,必须在已经保存的基础上进行,临时勾选启用点击生成是无效的! 第一步必须先修改保存状态！第二步点击生成!</p>
                                        <p>当您为自定义菜单填写链接地址时请填写以"https://"开头，这样可以保证用户手机浏览的兼容性更好</p>
                                    </div>
                                    <div class="row-fluid">

                                        <div class="span8 control-group">
                                            <a href="javascript:void(0)" class="btn" id="add_menu"><i class="icon-plus"></i>添加主菜单</a>
                                        </div>
                                    </div>

                                    <div class="row-fluid dataTables_wrapper">
                                        <form action="savemenu" method="post" class="form-horizontal form-validate" onsubmit="return checkbefore()">
                                            <table id="listTable" class="table">
                                                <thead>
                                                <tr>
                                                    <th>显示顺序</th>
                                                    <th>主菜单名称</th>
                                                    <th>触发关键词或链接地址</th>
                                                    <th>启用</th>
                                                    <th>操作</th>
                                                </tr>
                                                </thead>
                                            <% for(ptr in menus){%>
                                                <%if(ptr.pid=="0"){%>
                                                    <tr class="ptr">
                                                        <td>
                                                            <input type="text" class="input-mini" size="3" value="${ptr.sortno}" name="sortNo_${ptr.id}"   data-rule-number="true"/>
                                                        </td>
                                                        <td>
                                                            <input type="text" class="input-medium name" size="15" value="${ptr.main_menu}"  name="name_${ptr.id}" data-rule-required="true" data-rule-maxlength="30" />
                                                            <i class="icon-plus cursor_p add" title="添加子菜单" rel="${ptr.id}"></i>
                                                        </td>
                                                        <td>
                                                            <input type="text" class="input-medium type" size="15" value="${ptr.keyword_or_url}"  name="keyWord_${ptr.id}" data-rule-required="true" data-rule-maxlength="200" />
                                                            <input type="hidden" value="${ptr.pid}" name="parentId_${ptr.id}"/>
                                                            <input type="hidden" class="key_type" value="1" name="category_${ptr.id}">
                                                        </td>
                                                        <td>
                                                            <input class="ischecked" type="checkbox" value="${ptr.isonuse}" name="used_${ptr.id}"  <%if(ptr.isonuse==1){%>checked<%}%>/>
                                                            <input type="hidden" value="${ptr.id}"/>
                                                        </td>
                                                        <td>
                                                            <button type="button" class="btn btn-default scan_cash_register"  onclick="deleteMenu('${ptr.id}')">删除</button>
                                                        </td>
                                                    </tr>
                                                    <% for(ztr in menus){%>
                                                        <%if(ptr.id==ztr.pid){%>
                                                            <tr class="ztr">
                                                                <td>
                                                                    <input type="text" class="input-mini" size="3" value="${ztr.sortno}" name="sortNo_${ztr.id}"   data-rule-number="true"/>
                                                                </td>
                                                                <td>
                                                                    <i class='board'></i>
                                                                    <input type="text" class="input-medium name" size="15" value="${ztr.main_menu}"  name="name_${ztr.id}" data-rule-required="true" data-rule-maxlength="30" />
                                                                </td>
                                                                <td>
                                                                    <input type="text" class="input-medium type" size="15" value="${ztr.keyword_or_url}"  name="keyWord_${ztr.id}" data-rule-required="true" data-rule-maxlength="200" />
                                                                    <input type="hidden" value="${ztr.pid}" name="parentId_${ztr.id}"/>
                                                                    <input type="hidden" class="key_type" value="1" name="category_${ztr.id}">
                                                                </td>
                                                                <td>
                                                                    <input class="ischecked" type="checkbox" value="${ztr.isonuse}" name="used_${ztr.id}"  <%if(ztr.isonuse==1){%>checked<%}%>/>
                                                                    <input type="hidden" value="${ztr.pid}"/ >
                                                                </td>
                                                                <td>
                                                                    <button type="button" class="btn btn-default"  onclick="deleteMenu('${ztr.id}')">删除</button>
                                                                </td>
                                                            </tr>
                                                        <%}%>
                                                    <%}%>
                                                <%}%>
                                            <%}%>
                                            </table>
                                            <div>
                                                <input type="hidden" name="application_id" value="${application_id!}"><!--应用id-->
                                                <input type="hidden" name="apiid"  value="${apiid!}"><!--apiid-->
                                                <input type="hidden" name="_total" id="_total" value="${total_menu}">
                                                <button id="bsubmit" type="submit" data-loading-text="提交中..." class="btn btn-primary" style="background: #4b8cf7">保存</button>
                                                <button id="create_menu" type="button" class="btn btn-primary" style="background: #4b8cf7">生成自定义菜单</button>
                                            </div>
                                        </form>
                                        <script type="text/javascript">
                                            $().ready(function () {
                                                var $add_menu = $("#add_menu");
                                                var $add_zmenu = $("i.add");
                                                var $total_menu=$("#_total").val();
                                                var $menu_str="";
                                                var $menu_str0="";
                                                var $str="@";
                                                var $menu_index = 0;
                                                $add_menu.click(function (e) {
                                                    e.preventDefault();
                                                    $menu_index++;
                                                    $menu_str0=$str+$menu_index;
                                                    if($menu_index==1){
                                                        $menu_str=$total_menu+$menu_str+$menu_str0;
                                                    }else{
                                                        $menu_str=$menu_str+$menu_str0;
                                                    }
                                                    var _menuPtrtmp = '<tr class="ptr">'
                                                            + ' <td>'
                                                            + '  <input name="sortNo_' + $menu_index + '" size="3" type="text" value="0" class="input-mini"ata-rule-number="true" /></td>'
                                                            + ' <td>'
                                                            + '<input name="name_' + $menu_index + '" size="15" type="text" class="input-medium name" data-rule-required="true" data-rule-maxlength="30" /></td>'
                                                            + '<td>'
                                                            + ' <input name="keyWord_' + $menu_index + '" size="15" type="text" class="input-medium type" data-rule-required="true" data-rule-maxlength="500" />'
                                                            + ' <input type="hidden" name="parentId_' + $menu_index + '" value="{pid}" />'
                                                            + ' <input type="hidden" name="category_' + $menu_index + '" class="key_type" value="1" /></td>'
                                                            + ' <td>'
                                                            + '  <input type="checkbox" name="used_' + $menu_index + '" checked="checked" value="1"/></td>'
                                                            + ' <td><button class="btn btn-default del" value="'+$menu_str0+'">删除</button></td>'
                                                            + '</tr> ';
                                                    $("#listTable").append(_menuPtrtmp.replace("{pid}", 0));
                                                    $('#_total').val($menu_str);

                                                });
                                                $add_zmenu.click(function myfunction() {
                                                    var $pid = $(this).attr("rel");
                                                    var $thistr = $(this).parent().parent();
                                                    var next = $thistr.nextAll("tr");
                                                    $menu_index++;
                                                    $menu_str0=$str+$menu_index;
                                                    $menu_str=$menu_str+$menu_str0;
                                                    $('#_total').val($menu_str);
                                                    var _menuPtrtmp = '<tr class="ztr">'
                                                            + ' <td>'
                                                            + '  <input name="sortNo_' + $menu_index + '" size="3" type="text" value="0" class="input-mini"ata-rule-number="true" /></td>'
                                                            + ' <td>{z}'
                                                            + '<input name="name_' + $menu_index + '" size="15" type="text" class="input-medium name" data-rule-required="true" data-rule-maxlength="30" /></td>'
                                                            + '<td>'
                                                            + ' <input name="keyWord_' + $menu_index + '" size="15" type="text" class="input-medium type" data-rule-required="true" data-rule-maxlength="500" />'
                                                            + '  <input type="hidden" name="parentId_' + $menu_index + '" value="{pid}" />'
                                                            + '  <input type="hidden" name="category_' + $menu_index + '" class="key_type" value="1" /></td>'
                                                            + ' <td>'
                                                            + '  <input type="checkbox" name="used_' + $menu_index + '" checked="checked" value="1" /></td>'
                                                            + ' <td><button class="btn btn-default del" value="'+$menu_str0+'">删除</button></td>'
                                                            + '</tr> ';
                                                    var tp = _menuPtrtmp.replace("{pid}", $pid).replace("{z}", "<i class='board'></i>  ");
                                                    if (next.length > 0) {
                                                        next.first().before(tp);
                                                    } else {
                                                        $("#listTable").append(tp);
                                                    }
                                                });
                                                $("#listTable .del").live("click", (function () {
                                                    $(this).parents("tr").remove();
                                                    if($menu_str.endsWith($(this).attr("value"))){//以@X开头或者结尾
                                                        $menu_str=$menu_str.substring(0,$menu_str.lastIndexOf($(this).attr("value")));
                                                    }else{//@X在中间位置
                                                        $menu_str=$menu_str.replace($(this).attr("value")+$str,$str);
                                                    }

                                                    $('#_total').val($menu_str);
                                                }));
                                                $("#create_menu").click(function () {
                                                    var application_id =$("input[name='application_id']").val();
                                                    var apiid=$("input[name='apiid']").val();
                                                    if(!apiid){
                                                        alert("先在基础配置中保存应用id和应用秘钥");
                                                        return;
                                                    }
                                                    //验证一级菜单不能超过3个
                                                    var i=0;
                                                    $(".ptr .ischecked").each(function () {
                                                        if(this.checked==true){
                                                            ++i;
                                                        }
                                                    });
                                                    if(i>3){
                                                        alert("1级菜单最多只能开启3个!");
                                                        return;
                                                    }
                                                    //验证二级菜单不能超过5个
                                                    $(".ptr .ischecked").each(function () {//一级菜单
                                                        var j=0;
                                                        if(this.checked==true){
                                                            var pid=$(this).next().val();
                                                            $(".ztr .ischecked").each(function () {//二级菜单
                                                                if(this.checked==true){
                                                                    if(pid==$(this).next().val()){
                                                                        ++j;
                                                                    }
                                                                }
                                                            });
                                                        }
                                                        if(j>5){
                                                            alert("2级子菜单最多开启5个");
                                                            return;
                                                        }
                                                    });
                                                    $.ajax({
                                                        url:"/mall/sys/businessapp/createmenu",
                                                        type:"post",
                                                        dataType:"json",
                                                        data:{
                                                            "apiid":apiid,

                                                        },success:function(data){
                                                            if(data.code==0){
                                                                alert("创建成功！");
                                                                location.href = "custommenu";
                                                            }else{
                                                                alert(data.desc);
                                                                return;
                                                            }

                                                        }});
                                                });

                                            });
                                        </script>

                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>


        </div>

    </div>
</div>
</body>
<script type="text/javascript">
    function checkbefore(){
        //验证显示序号
        var input_mini=document.getElementsByClassName("input-mini");
        for( var i=0;i<input_mini.length;i++){
            if(!((/^(\+|-)?\d+$/.test( input_mini[i].value ))&&input_mini[i].value>=0)){
                alert("显示顺序请输入大于或等于0的整数！");
                return false;
            }
        }

        //验证菜单名称不能为空
        var input_medium_name=document.getElementsByClassName("input-medium name");
        for(var i=0;i<input_medium_name.length;i++){
            /* alert(input_medium[i].value);*/
            if(input_medium_name[i].value==""){
                alert("主菜单名称不能为空");
                return false;
            }
        }

        //验证除法关键词和链接地址不能为空
        var input_medium_type=document.getElementsByClassName("input-medium type");
        for(var i=0;i<input_medium_type.length;i++){
            /* alert(input_medium[i].value);*/
            if(input_medium_type[i].value==""){
                alert("触发关键词或链接地址不能为空");
                return false;
            }
        }
    }

    function deleteMenu(e) {
        if(confirm("您确定删除？")){
            window.location.href="deletemenu?id="+e+"";
        }
        return;
    }

    //菜单是否启动
    $(".ischecked").click(function () {
        if($(this).is(':checked')){
            $(this).attr("value",'1');
        }else{
            $(this).attr("value",'0');
        }

    });

</script>
</html>
